var output = "";
var starLng = "";
var starLat = "";
var endLng = "";
var endLat = "";
var acc = "";
var tokens = "";
document.addEventListener("plusready", onPlusReady, false);

function onPlusReady() {
	acc = plus.storage.getItem('account');
	tokens = plus.storage.getItem('token');
}

function getBasePrice() {
	$.ajax({
		type: "post",
		url: webRoot + "?s=/App/AppSystem/getBasePrice",
		success: function(d) {
			var dt = $.parseJSON(d);
			var data = dt.data;
			var starPrice = data.star; //起步價
			var durationPrice = data.duration; //里程價
			var subtractPrice = data.subtract; //夜裏行車價
			var nadds = parseInt(data.nadd); //免費等待時間
			var ends = $.trim($("#map_num").val());
			var star = $.trim($("#initial_position").val());

			var map = new BMap.Map("allmap");
			map.centerAndZoom(new BMap.Point(113.837, 22.745), 12);
			var searchComplete = function(results) {
				starLng = results.Ov.point.lng;
				starLat = results.Ov.point.lat;
				endLng = results.$u.point.lng;
				endLat = results.$u.point.lat;

				if(transit.getStatus() != BMAP_STATUS_SUCCESS) {
					return;
				}
				var plan = results.getPlan(0);

				//output += plan.getDuration(true) + "\n";                //获取时间
				//output += "总路程为：" ;
				output = plan.getDistance(true); //获取距离

			}

			var transit = new BMap.DrivingRoute(map, {
				renderOptions: {
					map: map
				},
				onSearchComplete: searchComplete,
				onPolylinesSet: function() {
					var mileage = parseFloat(output);
					var mileagePrice = parseFloat(mileage * durationPrice); //里程總費用
					var subtracts = parseFloat(subtractPrice * mileage); //夜間行車費
					//var waitPrices=parseFloat(parseFloat(waitPrice)*times);//等待價格
					var totalPrice = parseFloat(starPrice) + parseFloat(mileagePrice) - nadds;
					
					
					$("#sendOrder").on("click", function() {

	$.ajax({
		type: "post",
		url: webRoot + "?s=/App/Place/sendOrder",
		data: {
			account: acc,
			token: tokens,
			addresses: star, //上车地址
			down: ends, //下车地址
			latitude: starLng, //上车经度
			longitude: starLat, //上车纬度
			latitudel: endLng, //下车经度
			longitudel: endLat, //下车纬度
			cost: totalPrice //预估费用
		},
		success: function(d) {
			
			var data = $.parseJSON(d);
			console.log(JSON.stringify(data));
			var n = data.data.length;
			var ids=data.data.id;
			$("#OrderId").html(ids);
			alert(ids+",111");
			if(typeof(Storage) !== "undefined") {
						localStorage.setItem("oriderId",ids);
						
					}
			if(data.code == 200) {
				tool.openNewPage('Leaflets.html');
				
			} else {
				alert("5公里内没有司机");
			}

		}
	});

	
});
					
					
					
					setTimeout(function() {

						$(".pay").html(totalPrice);

					}, "1000");
				}
			});
			transit.search(star, ends);
		}
	});
}
getBasePrice();

